package com.adtech.adms.device.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.web.action.DataGridAction;
import com.adtech.adms.device.pojo.GuiDevice;
import com.adtech.adms.device.service.DeviceService;
import com.adtech.adms.system.pojo.Topo;
import com.adtech.adms.system.web.action.UserAction;
import com.adtech.core.dao.query.FilterMap;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.Sort;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.exception.SystemException;
import com.adtech.core.web.action.AjaxResult;

public class DeviceAction extends DataGridAction{
	private static final Logger log = Logger.getLogger(UserAction.class);
	private DeviceService deviceService;
	private GuiDevice guiDevice;
	private String topoIds;
	private String id;
	private String deviceKey;
	private String topoNames;
	
	public String getDeviceKey() {
		return deviceKey;
	}

	public void setDeviceKey(String deviceKey) {
		this.deviceKey = deviceKey;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public GuiDevice getGuiDevice() {
		return guiDevice;
	}

	public void setGuiDevice(GuiDevice guiDevice) {
		this.guiDevice = guiDevice;
	}

	public DeviceService getDeviceService() {
		return deviceService;
	}

	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}
	
	
	public String getTopoIds() {
		return topoIds;
	}

	public void setTopoIds(String topoIds) {
		this.topoIds = topoIds;
	}

	
	public String getTopoNames() {
		return topoNames;
	}

	public void setTopoNames(String topoNames) {
		this.topoNames = topoNames;
	}
	

	@Override
	protected List<?> getModelDataGridList(Pager pager, FilterMap filtermap,
			Sort sort) throws SystemException, ServiceException {
		// TODO Auto-generated method stub
		List<Topo> topos = (List<Topo>) session.get(SessionKey.SESSION_KEY_SELECT_TOPOS);
		return deviceService.getDeviceList(pager, filtermap.toExpression(), sort,topos);
	}

	public String main(){
		return SUCCESS;
	}
	
	public String save() {
		try {
//			guiDevice.setTopoSid(new Long(topoIds));
			deviceService.save(guiDevice);
			
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String getDetail() {
		if (id != null && !id.equals("")) {
			guiDevice = deviceService.getDevice(id);
			topoNames = generateTopoNames(guiDevice.getTopoSid());
		}
		return SUCCESS;
	}
	
	private String generateTopoNames(Long topoSid) {
		List<Topo> topos = (List<Topo>) session.get(SessionKey.SESSION_KEY_SELECT_TOPOS);
		for(Topo topo : topos){
			if(topo!=null && new Long(topo.getNodeid()).longValue() == topoSid.intValue()){
				
				return topo.getName();
			}
		}
		return null;
	}

	public String delete() {
			try {
				deviceService.delete(id);
				setAjaxResult(AjaxResult.success());
			} catch (ServiceException e) {
				log.error(e);
				setAjaxResult(AjaxResult.failed(e.getErrorCode(), e.getErrorMessage()));
			}
		return SUCCESS;
	}
	
	
}
